﻿using AutoMapper;
using MediatR;
using SmartProduction.Domain.yjh;
using SmartProduction.ErrorCode;
using SmartProduction.Infarstructrue.Interface;
using SmartProduction.Write.API.Application.Commands.WareHouser;

namespace SmartProduction.Write.API.Application.CommandHandlers.Warehoures
{
    /// <summary>
    /// 仓库添加命令处理
    /// </summary>
    public class WoreHouserCreateCommadHandler:IRequestHandler<WareHouserCreateCommand,APIResult<object>>
    {
        /// <summary>
        /// 仓库仓储
        /// </summary>
        private readonly IBaseRepository<WareHouse> warRep;

        /// <summary>
        /// mapper映射
        /// </summary>
        private readonly IMapper mapper;
        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="warRep">仓库仓储</param>
        /// <param name="mapper">mapper映射</param>
        public WoreHouserCreateCommadHandler(IBaseRepository<WareHouse> warRep,IMapper mapper)
        {
            this.warRep = warRep;
            this.mapper = mapper;
        }

        public async Task<APIResult<object>> Handle(WareHouserCreateCommand request, CancellationToken cancellationToken)
        {
            APIResult<object> result = new APIResult<object>();

            var entity=mapper.Map<WareHouse>(request);
            var r=warRep.AddAsync(entity).Result;

            if (r > 0)
            {
                LogModel logModel = new LogModel();
                logModel.CreateDate = DateTime.Now;
                logModel.CreatePerson = "YJH";
                logModel.LoginThing = "正在添加仓库";
                result.Code = APIEnums.添加成功;
                result.Msg=APIEnums.添加成功.ToString();

                result.data = r;    
            }
            else
            {
                result.Code = APIEnums.添加失败;
                result.Msg = APIEnums.添加失败.ToString();

                result.data = r;
            }

            return await Task.FromResult(result);
        }
    }
}
